package com.taoym.voucher.xcx.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taoym.voucher.common.constant.CommonConstant;
import com.taoym.voucher.common.constant.RedisKeyConstant;
import com.taoym.voucher.common.utils.HttpClientUtils;
import com.taoym.voucher.common.utils.ThreadLocalUtils;
import com.taoym.voucher.xcx.entity.dto.XcxECGetGoodsConvertDTO;
import com.taoym.voucher.xcx.entity.dto.XcxECGetGoodsDTO;
import com.taoym.voucher.xcx.entity.dto.XcxECGetRecommendGoodsDTO;
import com.taoym.voucher.xcx.entity.vo.XcxECGetGoodsConvertVO;
import com.taoym.voucher.xcx.entity.vo.XcxECGetGoodsVO;
import com.taoym.voucher.xcx.entity.vo.XcxECGetPlatformVO;
import com.taoym.voucher.xcx.service.SysUserService;
import com.taoym.voucher.xcx.service.XcxECommerceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @coder taoym
 * @datetime 2021/8/23 15:35
 * @desc
 */
@Service
@Slf4j
public class XcxECommerceServiceImpl implements XcxECommerceService {

    @Autowired
    private SysUserService sysUserService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Value("${jutuike.ec.goods}")
    private String ecGoodsUrl;
    @Value("${jutuike.ec.search}")
    private String ecSearchUrl;
    @Value("${jutuike.ec.platform}")
    private String ecPlatformUrl;
    @Value("${jutuike.ec.convert}")
    private String ecConvertUrl;
    @Value("${jutuike.ec.recommend}")
    private String ecRecommendUrl;

    @Override
    public List<XcxECGetGoodsVO> listECGoods(XcxECGetGoodsDTO data) {

        Map<String, Object> params = new HashMap<>();
        params.put("pub_id", sysUserService.getSysUser().getPubId());
        params.put("sid", ThreadLocalUtils.get().getUid());
        params.put("page", data.getPage());
        params.put("source", data.getSource());
        params.put("pageSize", data.getPageSize());
        params.put("sub_share_rate", data.getSubShareRate());

        if (data.getIsSearchByKeyword()) {
            params.put("keyword", data.getKeyword());
            params.put("sort", data.getSort());
        }

        String response = HttpClientUtils.doPost(data.getIsSearchByKeyword() ? ecSearchUrl : ecGoodsUrl, JSON.toJSONString(params));
        JSONObject jsonObject = JSON.parseObject(response);
        if (jsonObject.get("code").equals(1)) {
            return JSON.parseArray(JSON.toJSONString(jsonObject.get("data")), XcxECGetGoodsVO.class);
        }
        return null;
    }

    @Override
    public List<XcxECGetPlatformVO> listECPlatform() {

        Object redisObj = redisTemplate.opsForValue().get(RedisKeyConstant.XCX_EC_PLATFORM);
        if (!ObjectUtils.isEmpty(redisObj)) {
            return (List<XcxECGetPlatformVO>) redisObj;
        }

        String response = HttpClientUtils.doGet(ecPlatformUrl + "?pub_id=" + sysUserService.getSysUser().getPubId());
        JSONObject jsonObject = JSON.parseObject(response);
        if (jsonObject.get("code").equals(1)) {
            List<XcxECGetPlatformVO> ecGetPlatformVOS = JSON.parseArray(JSON.toJSONString(jsonObject.get("data")), XcxECGetPlatformVO.class);
            redisTemplate.opsForValue().set(RedisKeyConstant.XCX_EC_PLATFORM, ecGetPlatformVOS);
            return ecGetPlatformVOS;
        }
        return null;
    }

    @Override
    public XcxECGetGoodsConvertVO goodsConvert(XcxECGetGoodsConvertDTO data) {
        Map<String, Object> params = new HashMap<>();
        params.put("pub_id", sysUserService.getSysUser().getPubId());
        params.put("goodsId", data.getGoodsId());
        params.put("sid", ThreadLocalUtils.get().getUid());
        params.put("source", data.getSource());
        params.put("sub_share_rate", data.getSubShareRate());
        params.put("relation_flag_name", CommonConstant.RELATION_FLAG_NAME);
        String response = HttpClientUtils.doPost(ecConvertUrl, JSON.toJSONString(params));
        JSONObject jsonObject = JSON.parseObject(response);
        if (jsonObject.get("code").equals(1)) {
            return JSON.parseObject(JSON.toJSONString(jsonObject.get("data")), XcxECGetGoodsConvertVO.class);
        }
        return null;
    }

    @Override
    public List<XcxECGetGoodsVO> recommendGoods(XcxECGetRecommendGoodsDTO data) {
        Map<String, Object> params = new HashMap<>();
        params.put("pub_id", sysUserService.getSysUser().getPubId());
        params.put("goodsId", data.getGoodsId());
        params.put("source", data.getSource());
        String response = HttpClientUtils.doPost(ecRecommendUrl, JSON.toJSONString(params));
        JSONObject jsonObject = JSON.parseObject(response);
        if (jsonObject.get("code").equals(1)) {
            return JSON.parseArray(JSON.toJSONString(jsonObject.get("data")), XcxECGetGoodsVO.class);
        }
        return null;
    }
}
